﻿package Cain.LocalLoadSave
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	/**
	 * 本機載入圖檔
	 * 載入完成 "load_ok"
	 * @author Cain
	 */
	public class UpLoadPcx extends EventDispatcher
	{
		private const cain:String = "As3 code by 朱信曄 in 雄獅旅遊 2010 cainmaila@gmail.com";
		private var _file :FileReference; 
		private var _ldr :Loader;
		public function UpLoadPcx()
		{
			setup();
		}
		
		private function setup():void
		{
			_file = new FileReference();
			_ldr = new Loader();
		}
		/**
		 * 載入本機圖片
		 */
		public function loadPcx():void 
		{
			_file.addEventListener(Event.SELECT, onSelect);
			_file.browse([new FileFilter("Images Formats (*.jpg,*.gif,*.png)", "*.jpg;*.gif;*.png", "JPEG;jp2_;GIFF")]); 
		}
		
		private function onComplete(e:Event):void 
		{
			_file.removeEventListener(Event.COMPLETE, onComplete);
			var _data:ByteArray = _file.data; 
			_ldr.loadBytes(_data);
			dispatchEvent(new Event("load_ok"));
		}
		public function get loader():Loader 
		{
			_ldr.addEventListener(Event.ADDED_TO_STAGE, _ldronADDED_TO_STAGE);
			return _ldr;
		}
		
		private function _ldronADDED_TO_STAGE(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, _ldronADDED_TO_STAGE);
			var ty:TextField = new TextField();
			ty.text = cain;
			Sprite(_ldr.root).addChild(ty);
			ty.visible = false;
		}
		private function onSelect(e:Event):void 
		{
			_file.removeEventListener(Event.SELECT, onSelect);
			_file.addEventListener(Event.COMPLETE, onComplete);
			_file.load();
		}
	}
	
}